//
//  LBMEmoticonPageCell.swift
//  EmoticonKeyboard
//
//  Created by 刘保满 on 16/5/30.
//  Copyright © 2016年 itheima. All rights reserved.
//  

import UIKit

class LBMEmoticonPageCell: UICollectionViewCell {
    
    
    var emoticons: [LBMEmoticon]? {
        
        didSet {
            
            for (index,em) in emoticons!.enumerate() {
                
                let btn = buttonArray[index]
                btn.em = em
            }
            
            
        }
        
    }
    //定义按钮的数组
    lazy var buttonArray = [LBMEmoticonButton]()
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置UI
    private func setupUI() {
       
        //添加21个按钮
        for i in 0..<21 {
            
            let leftMargin: CGFloat = 5
            let bottomMargin: CGFloat = 30
            let w = (UIScreen.mainScreen().bounds.width - 2 * leftMargin) / CGFloat(EmoticonColCount)
            
            
            let h = (self.bounds.height - bottomMargin) / CGFloat(EmoticonRowCount)
            
            let btn = LBMEmoticonButton()
            btn.addTarget(self, action: #selector(LBMEmoticonPageCell.btnDidClick(_:)), forControlEvents: .TouchUpInside)
            
            btn.titleLabel?.font = UIFont.systemFontOfSize(32)

            let row = i / EmoticonColCount
            let col = i % EmoticonColCount
            
            let x: CGFloat = leftMargin + w * CGFloat(col)
            let y: CGFloat = CGFloat(row) * h
            btn.frame = CGRect(x: x, y: y, width: w, height: h)
            contentView.addSubview(btn)
            
            //把按钮添加到数组中
            buttonArray.append(btn)
            
        }
        
        //添加长按手势
        let longPress = UILongPressGestureRecognizer(target: self, action: #selector(LBMEmoticonPageCell.longPressFire(_:)))
        //添加手势
        self.contentView.addGestureRecognizer(longPress)
        
        
        
    }
    
    //按钮点击方法
    @objc private func btnDidClick(btn: LBMEmoticonButton) {
        
        //点击表情按钮的时候用通知传递数据
        //发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(EmoticonBtnDidClickNotification, object: btn.em)
        
        
    }
    
    //手势响应方法
    @objc private func longPressFire(ges: UILongPressGestureRecognizer) {
        //先获取手势触摸的点
        let point = ges.locationInView(contentView)
        
        //根据手势触摸的点,寻找对应的按钮
        guard let btn = buttonWithPoint(point) else {
            popView.dismissAter()
            
            return
        }
        //如果手势在空白按钮或者删除按钮上,就不要添加pop
        if btn.em!.isDelete || btn.em!.isEmpty {
            
            //移除pop
            popView.dismissAter()
            return
 
        }
        //根据手势的状态决定pop是否显示
        switch ges.state {
        case .Began,.Changed:
            
            popView.show(btn)
        default:
            popView.dismissAter()
        }

    }
    
    //根据手势触摸的点,去寻找对应的按钮
    private func buttonWithPoint(point: CGPoint) -> LBMEmoticonButton? {
        
        //需要遍历按钮的数组
        for btn in buttonArray {
            
            //去判断按钮的frame是否包含了触摸的点
            if btn.frame.contains(point) {
                
                return btn
            }
            
        }
        
        return nil
        
    }
    
    
    
    
    
    //懒加载pop视图
    private lazy var popView: LBMEmoticonPopView = {
       let pop = LBMEmoticonPopView.loadPopView()
        
        return pop
        
    }()
    
    
    
}
